<?php
/**
 * Класс обеспечивает формирование и хранение списка ошибок
 *
 * @author Дмитрий Кузьма <kouzma.d@gmail.com>
 * @version 1.0
 */
class Exception_ActiveRecord {
  /**
   * Список ошибок.
   *
   * @var array
   */
  public static $_listObjects = array();
  public static $_listFields = array();

  public function __construct() {
  }

  public static function addObjectException($className, $fieldName, $errText = 'error in the field', $complex = null) {
    if (is_array($className)) {
	    $strClassNames = '';
	    foreach ($className as $class) {
	    	$strClassNames .= '[\'' . $class . '\']';
	    }
    }
    if ($complex === null) {
      if (is_array($className)) {
        $s = "isset(self::\$_listObjects" . $strClassNames . "['" . $fieldName . "']) ? (self::\$_listObjects" . $strClassNames . "['" . $fieldName . "'] .= '; ' . \$errText) : (self::\$_listObjects" . $strClassNames . "['" . $fieldName . "'] = \$errText);";
  	    eval($s);  
      }
      else {
        if (isset(self::$_listObjects[$className][$fieldName])) self::$_listObjects[$className][$fieldName] .= '; ' . $errText;
        else self::$_listObjects[$className][$fieldName] = $errText;
      }
      
    }
    else {
    	if (isset(self::$_listObjects[$className][$complex][$fieldName])) self::$_listObjects[$className][$complex][$fieldName] .= '; ' . $errText;
      else self::$_listObjects[$className][$complex][$fieldName] = $errText;
    }
  }



  public static function addFieldException($fieldName, $errText = 'error in the field') {
   	self::$_listObjects[$fieldName] = $errText;
  }

  public static function isExceptionOfFieldExists($fieldName) {
  	if (isset(self::$_listObjects[$className][$fieldName]))
  	  return true;
	  else return false;
  }
  public static function isExceptionOfModelFieldExists($fieldName, $className) {
  	//if (isset(self::$_listFields[$fieldName]))
  	if (isset(self::$_listObjects[$className][$fieldName]))
  	  return true;
	  else return false;
  }

  public static function getExceptionOfField($fieldName) {
  	if (isset(self::$_listObjects[$fieldName]))
  	  return self::$_listObjects[$fieldName];
  	else return false;
  }

  public static function getExceptionOfModelField($fieldName, $className, $complex = null) {
    if (is_array($className)) {
	    $strClassNames = '';
	    foreach ($className as $class) {
	    	$strClassNames .= '[\'' . $class . '\']';
	    }
    }
    if ($complex !== null) {
  		if (isset(self::$_listObjects[$className][$complex][$fieldName]))
  	  return self::$_listObjects[$className][$complex][$fieldName];
  	}
  	else {
  	  if (is_array($className)) {
  	    $s = "\$return = isset(self::\$_listObjects" . $strClassNames . "['" . $fieldName . "']) ? ( self::\$_listObjects" . $strClassNames . "['" . $fieldName . "'] ) : false;";
  	    eval($s);  
  	    return $return;
  	    
  	  }
  	  else {
  	    if (isset(self::$_listObjects[$className][$fieldName])) return self::$_listObjects[$className][$fieldName];
  	    else return false;
  	  }
  	}
  	
  }

  public static function isExceptionOfModelExists($className) {
    if (isset(self::$_listObjects[$className]))
  	  return true;
	  else return false;
  }

  public static function getExceptionsOfModel($className) {
  	if (!isset(self::$_listObjects[$className]))
  	  return false;
		$retStr = '';
		foreach (self::$_listObjects[$className] as $val) {
		  if (is_array($val)) {
        $listError = false;;
  			$arrKeys = array_keys($val);
  			if($arrKeys && is_numeric($arrKeys[0])) {$listError = true;}
			
		    if ($listError) $retStr .= 'Не все списки заполнены корректно<br />';
			  else {
			    foreach ($val as $innerError) {
			    	$retStr .= $innerError . '<br />';
			    }
			  }
			}
			else
			  $retStr .= $val . '<br />';
		}
		return $retStr;
  }
  
  public static function getExceptionsOfModelToJs($className) {
  	if (!isset(self::$_listObjects[$className]))
  	  return false;
		$retStr = '';
		//array_key_exists()
		foreach (self::$_listObjects[$className] as $val) {
			$listError = false;;
			foreach (array_keys($val) as $valKeyName) {
				if (is_numeric($valKeyName)) $listError = true;;
			}
		  if ($listError)
			  $retStr .= "Not all lists are filled correctly\n";
			else
			  $retStr .= " - " . $val . "\n";
		}
		return $retStr;
  }

  public static function getArrExceptionOfModel($className) {
  	if (!isset(self::$_listObjects[$className])) return array();
  	else return self::$_listObjects[$className];
  }


  public static function addRowException($className, $errText = 'error in the field', $complex = null, $field = null) {
    if (is_array($className)) {
	    $strClassNames = '';
	    foreach ($className as $class) {
	    	$strClassNames .= '[\'' . $class . '\']';
	    }
    	if ($complex === null) {

    		//if (isset(self::$_listObjects[$className][$fieldName])) self::$_listObjects[$className][$fieldName] .= '; ' . $errText;
	      //else self::$_listObjects[$className][$fieldName] = $errText;
	    }
	    else {
	    	//$s = "\$obj = new " . $objName . "(\$element);";
	    	//$s = "\$link = isset(self::\$_listObjects" . $strClassNames . ") ? self::\$_listObjects" . $strClassNames . " : null;";// . "[\$complex] = \$errText";
	    	if (isset($field))
	    	$s = "isset(self::\$_listObjects" . $strClassNames . "[" . $complex . "]['" .$field. "']) ? (self::\$_listObjects" . $strClassNames . "[" . $complex . "]['" .$field. "'] .= '; ' . \$errText) : (self::\$_listObjects" . $strClassNames . "[" . $complex . "]['" .$field. "'] = \$errText);";// . "[\$complex] = \$errText";
	    	else
	    	$s = "isset(self::\$_listObjects" . $strClassNames . "[" . $complex . "]) ? (self::\$_listObjects" . $strClassNames . "[" . $complex . "] .= '; ' . \$errText) : (self::\$_listObjects" . $strClassNames . "[" . $complex . "] = \$errText);";// . "[\$complex] = \$errText";
	    	//echo $s;
	    	eval($s);
	    	//if (!$link)
	    	  //self::\$_listObjects" . $strClassNames . "


	    	//if (isset(self::$_listObjects[$className][$complex])) self::$_listObjects[$className][$complex] .= '; ' . $errText;
	      //else self::$_listObjects[$className][$complex] = $errText;
	    }
    }/*
    echo '<pre>';
    	print_r(self::$_listObjects);
    echo '</pre>';*/
  }


}
?>